/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.khuyenmai;

import dao.khuyenmaiDAO;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pojo.Chuongtrinhkhuyenmai;
import pojo.Hanghoa;
import util.Utils;

/**
 *
 * @author lhaquang
 */
public class ChuongTrinhKhuyenMaiJFrame extends javax.swing.JFrame {

    static Logger log = Logger.getLogger(ChuongTrinhKhuyenMaiJFrame.class.getName());
    private String hanhDong = "THEM";
    private DefaultListModel listModel;

    /**
     * Creates new form ChuongTrinhKhuyenMai
     */
    public ChuongTrinhKhuyenMaiJFrame(String _hanhDong) {
        initComponents();
        this.hanhDong = _hanhDong;
        this.jbtnHanhDong.setText(hanhDong);
        this.listModel = new DefaultListModel();
        this.jlDSMaHang.setModel(listModel);
    }
    public ChuongTrinhKhuyenMaiJFrame(Chuongtrinhkhuyenmai ctkm,String _hanhDong) {
        initComponents();
        this.hanhDong = _hanhDong;
        this.jbtnHanhDong.setText(hanhDong);
        this.jtfMaCTKM.setText(Integer.toString(ctkm.getMakhuyenmai()));
        
        this.listModel = new DefaultListModel();
        this.jlDSMaHang.setModel(listModel);
    }

    public void setElementForModel(List<Hanghoa> ds) {
        if (ds != null && ds.size() > 0) {
            for (int ix = 0; ix < ds.size(); ix++) {
                listModel.addElement(ds.get(ix));
            }
        }
    }

//    public ChuongTrinhKhuyenMaiJDialog(java.awt.Frame parent, boolean modal) {
//        super(parent, modal);
//        initComponents();
//        this.jbtnHanhDong.setText(hanhDong);
//    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfMaCTKM = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jlDSMaHang = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtaGhiChu = new javax.swing.JTextArea();
        jbtnThoat = new javax.swing.JButton();
        jbtnHanhDong = new javax.swing.JButton();
        jbtnChonHangHoa = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("CHUONG TRINH KHUYEN MAI");

        jLabel1.setText("MA CTKM");

        jLabel2.setText("BAT DAU");

        jLabel3.setText("KET THUC");

        jlDSMaHang.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jlDSMaHang);

        jtaGhiChu.setColumns(20);
        jtaGhiChu.setRows(5);
        jScrollPane2.setViewportView(jtaGhiChu);

        jbtnThoat.setText("THOAT");
        jbtnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThoatActionPerformed(evt);
            }
        });

        jbtnHanhDong.setText("HANH DONG");
        jbtnHanhDong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnHanhDongActionPerformed(evt);
            }
        });

        jbtnChonHangHoa.setText("Chon HH");
        jbtnChonHangHoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnChonHangHoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnThoat))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(82, 82, 82)
                                        .addComponent(jbtnHanhDong)
                                        .addGap(221, 221, 221))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(159, 159, 159)
                                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGap(181, 181, 181))
                                            .addComponent(jtfMaCTKM)))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane1)
                                    .addComponent(jbtnChonHangHoa, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))))))
                .addGap(32, 32, 32))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfMaCTKM, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jLabel2))
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnChonHangHoa)))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnThoat)
                    .addComponent(jbtnHanhDong))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThoatActionPerformed
        // TODO add your handling code here:
        log.info("Going to close frame: " + this.getName());
        this.dispose();
    }//GEN-LAST:event_jbtnThoatActionPerformed

    private void jbtnHanhDongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHanhDongActionPerformed
        // TODO add your handling code here:
        Chuongtrinhkhuyenmai ctkm = new Chuongtrinhkhuyenmai();
        ctkm.setMakhuyenmai(Utils.parse2Int(this.jtfMaCTKM.getText()));
        ctkm.setNgaybatdau(new Date());
        ctkm.setNgayketthuc(new Date());

        if (hanhDong.equalsIgnoreCase("THEM")) {
            khuyenmaiDAO.themChuongTrinhKhuyenMai(ctkm);
            JOptionPane.showMessageDialog(this, "Them thanh cong", "Thông báo", JOptionPane.NO_OPTION);
        } else if (hanhDong.equalsIgnoreCase("CAP NHAT")) {
            khuyenmaiDAO.capNhatChuongTrinhKhuyenMai(ctkm);
            JOptionPane.showMessageDialog(this, "Sua thanh cong", "Thông báo", JOptionPane.NO_OPTION);
        } else {
            log.error("Khong support hanh dong: " + hanhDong);
        }
        
        this.setVisible(false);
    }//GEN-LAST:event_jbtnHanhDongActionPerformed

    private void jbtnChonHangHoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnChonHangHoaActionPerformed
        // TODO add your handling code here:
        ChonHangHoaJDialog chhDialog = new ChonHangHoaJDialog(this, true);
        chhDialog.setVisible(true);
    }//GEN-LAST:event_jbtnChonHangHoaActionPerformed
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(ChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(ChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(ChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(ChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new ChuongTrinhKhuyenMai().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnChonHangHoa;
    private javax.swing.JButton jbtnHanhDong;
    private javax.swing.JButton jbtnThoat;
    private javax.swing.JList jlDSMaHang;
    private javax.swing.JTextArea jtaGhiChu;
    private javax.swing.JTextField jtfMaCTKM;
    // End of variables declaration//GEN-END:variables
}
